package cn.lanchengkeji.datafillingsystem.controller;


import cn.lanchengkeji.datafillingsystem.common.Const;
import cn.lanchengkeji.datafillingsystem.common.ResponseBean;
import cn.lanchengkeji.datafillingsystem.common.StatusCode;
import cn.lanchengkeji.datafillingsystem.entity.RqGroup;
import cn.lanchengkeji.datafillingsystem.entity.RqTask;
import cn.lanchengkeji.datafillingsystem.entity.RqTemplate;
import cn.lanchengkeji.datafillingsystem.entity.Users;
import cn.lanchengkeji.datafillingsystem.service.IRqGroupService;
import cn.lanchengkeji.datafillingsystem.service.IRqTaskService;
import cn.lanchengkeji.datafillingsystem.service.IRqTemplateService;
import cn.lanchengkeji.datafillingsystem.service.IUsersService;
import cn.lanchengkeji.datafillingsystem.utils.Utils;
import cn.lanchengkeji.datafillingsystem.vo.RqTaskVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 模板任务 前端控制器
 * </p>
 *
 * @author bobobobobo
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/rq-task")
public class RqTaskController extends BaseController {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(UploadImgController.class);

	@Resource
	private IRqTaskService iRqTaskService;

	@Resource
	private IUsersService iUsersService;

	@Resource
	private IRqGroupService iRqGroupService;

	@Resource
	private IRqTemplateService iRqTemplateService;


	/**
	 * 任务分页查询
	 * @param rqTaskVO
	 * @param currentPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/page")
	@RequiresAuthentication
	public ResponseBean page(@RequestBody RqTaskVO rqTaskVO,
	                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
	                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
		// 获取登陆系统用户名，存入日志
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		IPage<RqTaskVO> page = new Page(currentPage, pageSize);
		IPage<RqTaskVO> iPage = iRqTaskService.pageRqTask(page , rqTaskVO);
		return new ResponseBean(StatusCode.OK, iPage);
	}


	/**
	 * 任务审批
	 * @param rqTaskVO
	 * @param currentPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageSp")
	@RequiresAuthentication
	public ResponseBean pageSp(@RequestBody RqTaskVO rqTaskVO,
	                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
	                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
		// 获取登陆系统用户名，存入日志
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);
		IPage<RqTaskVO> page = new Page(currentPage, pageSize);
		page = iRqTaskService.pageSp(page , rqTaskVO , user.getId());
		return new ResponseBean(StatusCode.OK, page);
	}

	/**
	 * 新建任务
	 * @param rqTaskVO
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@Transactional
	@RequiresAuthentication
	public ResponseBean create(@RequestBody RqTaskVO rqTaskVO)throws Exception {
		// 获取登陆系统用户名，存入日志
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);
		Boolean ret = false;

		if(rqTaskVO.getUserType().equals(1)){
			List<RqGroup> userGroupList = rqTaskVO.getUserGroupList();
			for (RqGroup rqGroup : userGroupList) {
				rqTaskVO.setInitiator(user.getId());
				rqTaskVO.setInitiatorDepartment(user.getDepartment());
				rqTaskVO.setResponsibilityGroup(rqGroup.getId());
				rqTaskVO.setResponsibilityPeo(rqGroup.getTeamLeader());
				rqTaskVO.setCreateTime(new Date());
				rqTaskVO.setCreateUser(user.getId());
				ret = iRqTaskService.save(rqTaskVO);
			}
		}else{
			List<Users> usersList = rqTaskVO.getUsersList();
			for (Users users : usersList) {
				rqTaskVO.setInitiator(users.getId());
				rqTaskVO.setInitiatorDepartment(user.getDepartment());
				rqTaskVO.setCreateTime(new Date());
				rqTaskVO.setCreateUser(user.getId());
				ret = iRqTaskService.save(rqTaskVO);
			}
		}

		return new ResponseBean(StatusCode.OK, ret);
	}


	/**
	 * 分页查询小组列表
	 * @param rqTaskVO
	 * @param currentPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageRqGroup")
	@RequiresAuthentication
	public ResponseBean pageRqGroup(@RequestBody RqGroup rqTaskVO,
	                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
	                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
		// 获取登陆系统用户名，存入日志
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);
		QueryWrapper<RqGroup> rqGroupQueryWrapper = new QueryWrapper<>();
		rqGroupQueryWrapper.eq(RqGroup.DEL_FLAG , Const.DEL_FLAG_NO);
		IPage<RqGroup> page = new Page(currentPage, pageSize);
		IPage<RqGroup> iPage = iRqGroupService.page(page, rqGroupQueryWrapper);
		return new ResponseBean(StatusCode.OK, iPage);
	}


	/**
	 * 分页查询指派人
	 * @param rqTaskVO
	 * @param currentPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageUsers")
	@RequiresAuthentication
	public ResponseBean pageUsers(@RequestBody RqGroup rqTaskVO,
	                                @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
	                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
		// 获取登陆系统用户名，存入日志
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);
		QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(Users.DEL_FLAG , Const.DEL_FLAG_NO);
		IPage<Users> page = new Page(currentPage, pageSize);
		IPage<Users> iPage = iUsersService.page(page, queryWrapper);
		return new ResponseBean(StatusCode.OK, iPage);
	}


	/**
	 * 模板分页查询
	 * @param rqTemplate
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageRqTemplate")
	@RequiresAuthentication
	public ResponseBean pageRqTemplate(@RequestBody RqTemplate rqTemplate) throws Exception {
		// 获取登陆系统用户名，存入日志
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);

		QueryWrapper<RqTemplate> rqTemplateQueryWrapper = new QueryWrapper<>();
		rqTemplateQueryWrapper.eq(RqTemplate.DEL_FLAG , Const.DEL_FLAG_NO);
		rqTemplateQueryWrapper.eq(RqTemplate.IS_CHECK , Const.IS_CHECK_YES);
		rqTemplateQueryWrapper.eq(RqTemplate.IS_RELEASE , Const.IS_RELEASE_YES);
		rqTemplateQueryWrapper.eq(RqTemplate.IS_START , Const.IS_START_YES);
		List<RqTemplate> list = iRqTemplateService.list( rqTemplateQueryWrapper);
		return new ResponseBean(StatusCode.OK, list);
	}


	/**
	 * 任务审核
	 * @param rqTaskVO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@Transactional
	@RequiresAuthentication
	public ResponseBean check(@RequestBody RqTaskVO rqTaskVO)throws Exception {
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);

		//todo 调用润乾api
		//todo 调用工作流

		QueryWrapper<RqTask> rqTaskQueryWrapper = new QueryWrapper<>();
		rqTaskQueryWrapper.eq(RqTask.ID , rqTaskVO.getId());
		RqTask one = iRqTaskService.getOne(rqTaskQueryWrapper);
		if(Utils.notEmpty(rqTaskVO.getRemark())){
			one.setRemark(rqTaskVO.getRemark());
			one.setAuditState(rqTaskVO.getAuditState());
		}else{
			one.setAuditState(rqTaskVO.getAuditState());
		}
		boolean ret = iRqTaskService.updateById(one);

		return new ResponseBean(StatusCode.OK, ret);
	}


	/**
	 * 已填报
	 * @param rqTaskVO
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/fillRateYes", method = {RequestMethod.GET, RequestMethod.POST})
	@Transactional
	public ResponseBean fillRate(@RequestBody RqTaskVO rqTaskVO , HttpServletResponse response ) throws Exception {

		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);

		List<RqTaskVO> list = iRqTaskService.fillRateYes(rqTaskVO);


		return new ResponseBean(StatusCode.OK, list);

	}



	/**
	 * 分页查看我得任务
	 * @param rqTaskVO
	 * @param currentPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageByTb")
	@RequiresAuthentication
	public ResponseBean pageByTb(@RequestBody RqTaskVO rqTaskVO,
	                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
	                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) throws Exception {
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);

		IPage<RqTaskVO> page = new Page(currentPage, pageSize);
		IPage<RqTaskVO> iPage = iRqTaskService.pageByTb(page , rqTaskVO);
		return new ResponseBean(StatusCode.OK, iPage);
	}




	/**
	 * 已审核
	 * @param rqTaskVO
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/checked", method = {RequestMethod.GET, RequestMethod.POST})
	@Transactional
	public ResponseBean checked(@RequestBody RqTaskVO rqTaskVO , HttpServletResponse response ) throws Exception {
		Object obj = SecurityUtils.getSubject().getPrincipal();
		logger.debug((String) obj);
		Users user = iUsersService.getUserByName((String) obj);

		List<RqTaskVO> list = iRqTaskService.checked(rqTaskVO);

		return new ResponseBean(StatusCode.OK, list);
	}


}
